package com.graduation.sfstore.event;

import com.alibaba.fastjson.JSONObject;
import com.graduation.sfstore.entity.Event;
import com.graduation.sfstore.entity.Message;
import com.graduation.sfstore.entity.TemperatureData;
import com.graduation.sfstore.service.MessageService;
import com.graduation.sfstore.service.TemperatureService;
import com.graduation.sfstore.util.SFWarehouseConstant;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class EventConsumer implements SFWarehouseConstant {

    private static final Logger logger = LoggerFactory.getLogger(com.graduation.sfstore.event.EventConsumer.class);

    @Autowired
    private MessageService messageService;

    @Autowired
    private TemperatureService temperatureService;


    @KafkaListener(topics = {TOPIC_OUTWAREHOUSE,TOPIC_INWAREHOUSE})
    public void handleCommentMessage(ConsumerRecord record) {
        if (record == null || record.value() == null) {
            logger.error("消息的内容为空!");
            return;
        }

        Event event = JSONObject.parseObject(record.value().toString(), Event.class);
        if (event == null) {
            logger.error("消息格式错误!");
            return;
        }

        // 发送站内通知
        Message message = new Message();
        message.setFromId(event.getUserId());
        message.setToId(1);
        message.setConversationId(event.getTopic());
        message.setCreateTime(event.getDate());
        message.setFishname(event.getFishname());
        message.setWeight(event.getWeight());

        Map<String, Object> content = new HashMap<>();
        content.put("userId", event.getUserId());
        content.put("entityType", event.getEntityType());
        content.put("entityId", event.getEntityId());

        if (!event.getData().isEmpty()) {
            for (Map.Entry<String, Object> entry : event.getData().entrySet()) {
                content.put(entry.getKey(), entry.getValue());
            }
        }

        message.setContent(JSONObject.toJSONString(content));
        messageService.addMessage(message);
    }



    @KafkaListener(topics = {TOPIC_RECEIVE})
    public void handleTempMessage(ConsumerRecord record) {
        if (record == null || record.value() == null) {
            logger.error("消息的内容为空!");
            return;
        }

        Event event = JSONObject.parseObject(record.value().toString(), Event.class);
        if (event == null) {
            logger.error("消息格式错误!");
            return;
        }

        TemperatureData temperatureData = new TemperatureData();
        String material = event.getFishname();
        String temperature = material.substring(1,3);
        System.out.println(temperature);
        String humidity = material.substring(3,5);
        System.out.println(humidity);

        temperatureData.setTemperature(temperature);
        temperatureData.setHumidity(humidity);
        temperatureData.setMaterial(material);
        temperatureService.insertTemperature(temperatureData);

    }
}
